﻿$(document).ready(function () {
    $('span[data-role-report="paging"]').bind('click', function () {
        pagingReport($(this));
    });

    $('span[data-role-report="next"]').bind('click', function () {
        $('[data-role-report="prev"]').show();
        var currentPaging = parseInt($('#pagingReport [data-role-report="paging"]').last().text());
        var pages = parseInt($('#pagingReport').data('data'));
        $('#pagingReport [data-role-report="paging"]').hide();
        var j = 0;
        for (var i = currentPaging + 1; i <= pages && i <= currentPaging + 3; i++) {
            $('#pagingReport [data-role-report="paging"]').eq(j).show().text(i);
            j++;
        }
        if (pages <= currentPaging + 3) {
            $(this).hide();
        }
        $('#pagingReport  [data-role-report="paging"]').removeClass('page-act');
        $('#pagingReport  [data-role-report="paging"]').filter(':contains(' + currentPage + ')').addClass('page-act');
        //pagingReport($('#pagingReport a[data-role-report="paging"]').first());
    });
    $('span[data-role-report="prev"]').bind('click', function () {
        $('[data-role-report="next"]').show();
        var currentPaging = parseInt($('#pagingReport [data-role-report="paging"]').first().text());
        var pages = parseInt($('#pagingReport').data('data'));
        $('#pagingReport [data-role-report="paging"]').hide();
        var j = 2;
        for (var i = currentPaging - 1; i >= 1 && i >= currentPaging - 3; i--) {
            $('#pagingReport [data-role-report="paging"]').eq(j).show().text(i);
            j--;
        }
        if (currentPaging - 3 <= 1) {
            $(this).hide();
        }
        $('#pagingReport  [data-role-report="paging"]').removeClass('page-act');
        $('#pagingReport  [data-role-report="paging"]').filter(':contains(' + currentPage + ')').addClass('page-act');
        //  pagingReport($('#pagingReport a[data-role-report="paging"]').first());
    });
});
function pagingReport(obj) {
    window.location.href =   'http://phoamthuc.net/Nhahang.aspx?page=' + $(obj).text();
}

function createPagingReport(numberPages, idCtrl) {
    $('#' + idCtrl).data('data', numberPages);
    $('#' + idCtrl + ' span').hide();
    var temp = currentPage % 3;
    var j, k, f;
    if (temp == 0) {
        j = k = f = Math.floor(currentPage / 3) * 3 - 2;
    }
    else {
        j = k = f = Math.floor(currentPage / 3) * 3 + 1;
    }
    $('#' + idCtrl + ' [data-role-report="paging"]').each(function () {
        $(this).text(j);
        j++;
    });
    for (var i = 0; k <= numberPages; i++) {
        k++;
        $('#' + idCtrl + ' [data-role-report="paging"]').eq(i).show();
    }
    if (numberPages - f > 3) {
        $('#' + idCtrl + ' [data-role-report="next"]').show();
    }
    if (f > 3) {
        $('#' + idCtrl + ' [data-role-report="prev"]').show();
    }
    $('#' + idCtrl + ' [data-role-report="paging"]').filter(':contains(' + currentPage + ')').addClass('page-act');
}
